<?php
//echo "<pre>";
//print_r($engineReturn);
$value		=	$engineReturn['record']['value'];
$source		=	$engineReturn['record']['source'];
$fieldname	=	$engineReturn['record']['fieldname'];
$fieldIdVal	=	$engineReturn['record']['fieldidvalue'];
$attr		=	$engineReturn['record']['attr'];
if($engineReturn['record']['interface']=='edit')
{
?>
<div class="divedit" style="display:none">
<?php
	if(is_array($engineReturn['record']['msg']))
	echo "<div>".$engineReturn['record']['msg'][0]."</div>";
?>
<form id="frmEngineInsert" name="frmEngineInsert" class="subeditclass" method="post" action="#" enctype="multipart/form-data">
<?php
		switch($engineReturn['record']['input'])
		{
			case "text":
				echo "<input type='text' id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."' class='rejectChar engineInput ".(($attr['maxlength']==10)?"datepicker":"")."' size='".$attr['size']."' maxlength='".$attr['maxlength']."' title='Input Text' value='".$value."'>";
			break;
			case "text_area":
				echo "<textarea id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."' class='rejectChar engineInput' cols='".$attr['cols']."' rows='".$attr['rows']."' title='Input Text'>".$value."</textarea>";
			break;
			case "radio":
				if(is_array($value))
					$value = $value[0];
				echo "<table>";
				foreach($source as $rs)
				{
					echo "<tr><td>";
						echo "<input type='radio' id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."' value='".$rs[0]."' ".(($value!=$rs[0])?"checked='checked'":"")."> ".(isset($rs[1])?$rs[1]:$rs[0]);
					echo "</td></tr>";
				}
				echo "</table>";
			break;
			case "select":
				echo "<select id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."'>";
				echo "<option value=''>-</option>";
				foreach($source as $rs)
				{
						echo "<option value='".$rs[0]."' ".(($value[0]==$rs[0])?"selected='selected'":"")."> ".(isset($rs[1])?$rs[1]:$rs[0])."</option>";
				}
				echo "</select>";
			break;
			case "simple_table":
				echo "<select id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."'>";
				echo "<option value=''>--</option>";
				foreach($source as $rs)
				{
						echo "<option value='".$rs['idRec']."'".(($value['idRec']==$rs['idRec'])?"selected='selected'":"")."> ".$rs['dataRec']."</option>";
				}
				echo "</select>";
			break;
			case "medium_table":
					$valueRec	= "";
//					echo "<pre>";
//					print_r($value);
					if(is_array($value))
					{
						$valueId	= $value[0];
						$valueRec	= $value[1];
					}
					else
					{
						$valueId	= "";
					}
					echo "<input type='hidden' id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."' value='".$valueId."'>";
					echo "<div  id='container-".$fieldname."-".$fieldIdVal."' name='container-".$fieldname."-".$fieldIdVal."' class='engineInputContainer'>";
						echo "<div id='edit-".$fieldname."-".$fieldIdVal."' name='edit-".$fieldname."-".$fieldIdVal."' class='defContainer' title='Press here to switch beetwen Search and View Mode'>";
							echo "<a href='#' class='engineInputEdit' id='edit-button-".$fieldname."-".$fieldIdVal."' name='edit-button-".$fieldname."-".$fieldIdVal."'>Edit</a>";
						echo "</div>";

						echo "<div id='search-".$fieldname."-".$fieldIdVal."' name='search-".$fieldname."-".$fieldIdVal."' class='defContainer' style='display:none;'>";
							echo "<input type='text' id='search-input-".$fieldname."-".$fieldIdVal."' name='search-input-".$fieldname."-".$fieldIdVal."' class='rejectChar engineInput' size='' maxlength=''>";
							echo "<div id='ajax-".$fieldname."-".$fieldIdVal."' name='ajax-".$fieldname."-".$fieldIdVal."' class='containerAjax'></div>";
						echo "</div>";

						echo "<div  id='view-".$fieldname."-".$fieldIdVal."' name='view-".$fieldname."-".$fieldIdVal."' class='engineInputView'>";
							if(is_array($valueRec) && ($valueId))
							{
								echo "<table class='table'>";
									echo "<tr>";
									foreach($valueRec as $v)
									{
										echo "<th>$v</th>";
									}
									echo "</tr>";
									if(isset($valueId[0]))
									{
										echo "<tr>";
										foreach($valueId[0] as $v)
										{
											echo "<td>$v</td>";
										}
										echo "</tr>";
									}
								echo "</table>";
							}
						echo "</div>";
					echo "</div>";
					echo "<script type='text/javascript'>";
					echo "$(document).ready(function(){";
					echo "$('#edit-button-".$fieldname."-".$fieldIdVal."').loadMediumTable({
					module_name 		: '".$fieldname."-".$fieldIdVal."'
					, search_div_id 	: 'search-".$fieldname."-".$fieldIdVal."'
					, search_input_id 	: 'search-input-".$fieldname."-".$fieldIdVal."'
					, view_div_id		: 'view-".$fieldname."-".$fieldIdVal."'
					, ajax_div_id		: 'ajax-".$fieldname."-".$fieldIdVal."'
					, url				: '".$baseUrlSave.$otority."/engine/".$engineReturn['modulename']."/ajax/medium_table/".$fieldname."-".$fieldIdVal."/'});";
					echo "});";
					echo "</script>";
			break;
			case "advance_table":
					$valueRec	= "";
//					echo "<pre>";
//					print_r($value);
					if(is_array($value))
					{
						$valueId	= $value[0];
						$valueRec	= $value[1];
					}
					else
					{
						$valueId	= "";
					}
					print_r($valueId);
					echo "<input type='hidden' id='".$fieldname."-".$fieldIdVal."' name='".$fieldname."-".$fieldIdVal."' value='".$valueId."'>";
					echo "<div  id='container-".$fieldname."-".$fieldIdVal."' name='container-".$fieldname."-".$fieldIdVal."' class='engineInputContainer'>";
						echo "<div id='edit-".$fieldname."-".$fieldIdVal."' name='edit-".$fieldname."-".$fieldIdVal."' class='defContainer'>";
							echo "<a href='#' class='engineInputEdit' id='edit-button-".$fieldname."-".$fieldIdVal."' name='edit-button-".$fieldname."-".$fieldIdVal."' title='Press here to switch beetwen Search and View Mode'>Edit</a>";
						echo "</div>";

						echo "<div id='search-".$fieldname."-".$fieldIdVal."' name='search-".$fieldname."-".$fieldIdVal."' class='defContainer' style='display:none;'>";
							echo "<input type='text' id='search-input-".$fieldname."-".$fieldIdVal."' name='search-input-".$fieldname."-".$fieldIdVal."' class='rejectChar engineInput' size='' maxlength=''>";
							if(is_array($source))
							{
								echo "<div id='filter-".$fieldname."-".$fieldIdVal."' name='filter-".$fieldname."-".$fieldIdVal."' class='containerFilter'>";
								echo "<h3>Filter</h3>";
								echo "<table class='table'>";
								foreach($source as $k=>$r)
								{
									echo "<tr>";
									echo "<td><b>".$r['text']."</b></td>";
									echo "<td>";
									switch($r['input'])
									{
										case "text":
											echo "<input type='text' id='filter-id-".$k."' name='filter-id-".$k."' class='engineInput' size='30' maxlength='100'>";
										break;
										case "radio":
											echo "<table>";
											foreach($r['source'] as $rs)
											{
												echo "<tr><td>";
													echo "<input type='radio' id='filter-id-".$k."' name='filter-id-".$k."' value='".$rs[0]."'> ".(isset($rs[1])?$rs[1]:$rs[0]);
												echo "</td></tr>";
											}
											echo "</table>";
										break;
										case "select":
											echo "<select id='filter-id-".$k."' name='filter-id-".$k."'>";
											echo "<option value=''>-".$r['text']."-</option>";
											foreach($r['source'] as $rs)
											{
													echo "<option value='".$rs[0]."'> ".(isset($rs[1])?$rs[1]:$rs[0])."</option>";
											}
											echo "</select>";
										break;
										case "simple_table":
											echo "<select id='filter-id-".$k."' name='filter-id-".$k."'>";
											echo "<option value=''>-".$row['text']."-</option>";
											foreach($r['source'] as $rs)
											{
													echo "<option value='".$rs['idRec']."'> ".$rs['dataRec']."</option>";
											}
											echo "</select>";
										break;
									}
									echo "</td>";
								}
								echo "</table>";
								echo "</div>";
							}
							echo "<div id='ajax-".$fieldname."-".$fieldIdVal."' name='ajax-".$fieldname."-".$fieldIdVal."' class='containerAjax'></div>";
						echo "</div>";

						echo "<div  id='view-".$fieldname."-".$fieldIdVal."' name='view-".$fieldname."-".$fieldIdVal."' class='engineInputView'>";
							if(is_array($valueRec) && ($valueId))
							{
								echo "<table class='table'>";
									echo "<tr>";
									foreach($valueRec as $v)
									{
										echo "<th>$v</th>";
									}
									echo "</tr>";
									if(isset($valueRec[0][0]))
									{
										echo "<tr>";
										foreach($valueId[0] as $v)
										{
											echo "<td>$v</td>";
										}
										echo "</tr>";
									}
								echo "</table>";
							}
						echo "</div>";
					echo "</div>";
					echo "<script type='text/javascript'>";
					echo "$(document).ready(function(){";
					echo "$('#edit-button-".$fieldname."-".$fieldIdVal."').loadAdvanceTable({
					module_name 		: '".$fieldname."-".$fieldIdVal."'
					, search_div_id 	: 'search-".$fieldname."-".$fieldIdVal."'
					, search_input_id 	: 'search-input-".$fieldname."-".$fieldIdVal."'
					, view_div_id		: 'view-".$fieldname."-".$fieldIdVal."'
					, ajax_div_id		: 'ajax-".$fieldname."-".$fieldIdVal."'
					, filter_div_id 	: 'filter-".$fieldname."-".$fieldIdVal."'
					, filter_count		: '".count($source)."'
					, url				: '".$baseUrlSave.$otority."/engine/".$engineReturn['modulename']."/ajax/advance_table/".$fieldname."-".$fieldIdVal."/'});";
					echo "});";
					echo "</script>";
			break;
		}
?>
        	<input type="submit" value="Save" class="button" id="submitInsert">
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
	$('.divedit').show('slow');
	$('.subeditclass').saveSubEditData({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/single_edit/<?php echo $fieldIdVal; ?>/<?php echo $fieldname;?>/',
		field_id		: '<?php echo $fieldname."-".$fieldIdVal; ?>',
		type			: '<?php echo $engineReturn['record']['input'];?>'
		});
<?php
if($engineReturn['record']['input']=='radio')
	echo "$('.subeditclass').submit();";
?>
});
</script>
<?php
}
else
{
	echo "<div class='columnGrid'>";
		echo "<a idrecord='$fieldIdVal' fieldname='$fieldname' href='#' class='editIcon'>";
		echo "<img src='".$engineReturn['admin_template']."css/images/edit.png' />";
		echo "</a>";
	echo "</div>";
	echo "<div class='ingridclass' style='display:none'>";
	switch($engineReturn['record']['input'])
	{
	case "hidden":
	case "text":
	case "text_primary":
	case "password":
	case "text_area":
	case "text_area_plus_editor":
		echo $value;
	break;
	case "checkbox":
		if(is_array($value))
		{
			foreach($value as $v)
			{
				echo $v.", ";
			}
		}
	break;
	case "image":
		if($res[$i][$fieldName]!="")
			echo "<div class='defContainerImage'>"
				."<img src='".$res[$i][$fieldName]."'>"
				."</div>";
	break;
	case "radio":
	case "select":
		if(isset($value[1]))
			echo $value[1];
		else
			echo $value[0];
	break;
	case "simple_table":
		if(isset($value['dataRec']))
			echo $value['dataRec'];
		else
			echo "-";
	break;
	case "medium_table":
	case "advance_table":
		if(isset($value[1]))
			$rs = count($value[1]);
		echo "<table>";
		echo "<tr>";
			if(isset($value[1]) && (is_array($value[1])))
			{
				foreach($value[1] as $t)
				{
					echo "<td>".$t."</td>";
				}
			}
			echo "</tr>";
			echo "<tr>";
			if(isset($value[0][0]) && (is_array($value[0][0])))
			{
				foreach($value[0][0] as $t)
				{
					echo "<td>".$t."</td>";
				}
			}
			else
			{
				echo "<td colspan='$rs'>";
			}
			echo "</tr>";
			echo "</table>";
		break;
	}
	echo "</div>";
	?>
<script type="text/javascript">
	$('.ingridclass').show('slow');
	$('.editIcon').navigateToEditDataNormal({
		url 			: '<?php echo $baseUrlSave;?><?php echo $otority;?>/engine/<?php echo $engineReturn['modulename'] ?>/ajax/single_edit/',
		});
</script>
    <?php
}
?>
